package com.superzlc.miniweb.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import com.superzlc.miniweb.comn.Configuation;
import com.superzlc.miniweb.comn.Util;
import com.superzlc.miniweb.servlet.anno.Controller;
import com.superzlc.miniweb.servlet.anno.QueryParam;
import com.superzlc.miniweb.servlet.anno.RequestMapping;

@Controller
public class Download {

	@RequestMapping(value="/download")
	public void download(HttpServletResponse response, @QueryParam("file") String downLoadPath) throws IOException {
		String downloadDir = getDownloadHome();
		if (downLoadPath == null || downLoadPath.length() == 0) {
			response.sendError(900, "没有指定下载的文件");
			return;
		}
		File file = new File(downloadDir + File.separator + downLoadPath);
		System.out.println("下载文件：" + file.getPath());
		if (!file.exists() || !file.isFile()) {
			response.sendError(900, "文件不存在或文件不是字节文件");
			return;
		}
		Util._download(response, file);
	}

	public static String getDownloadHome() {
		return Configuation.getString("download_home");
	}

}
